package com.qikux.controller;


import cn.hutool.core.io.FileUtil;
import com.qikux.annotation.*;
import com.qikux.annotition.Authentication;
import com.qikux.consts.AppConst;
import com.qikux.entity.User;
import com.qikux.service.MessageService;
import com.qikux.utils.ContextUtils;
import com.qikux.utils.UploadUtils;
import com.qikux.vo.HttpEntity;
import com.qikux.vo.Page;
import com.qikux.vo.ResultVo;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;


@Component
@RequestMapping(value = "/msg")
public class MessageController {
    @Resource
    private MessageService megService;

    String pt = this.getClass().getResource("/").getPath().replaceFirst("/", "");

    String webappRoot = pt.replaceAll("target/downloadLover-1.0-SNAPSHOT/WEB-INF/classes/", "src/main/webapp/upload");

    /**
     * @description 跳转聊天页面
     * @date 2022/9/28 21:50
     * @auther XuHuanDong
     */
    @RequestMapping(value = "/chat",method = RequestMethod.GET)
    @Authentication
    public String chat(){
        return "chat";
    }

    /**
     * @description 跳转消息通知页
     * @date 2022/9/28 21:50
     * @auther XuHuanDong
     */
    @RequestMapping(value = "/message",method = RequestMethod.GET)
    @Authentication
    public String message(){
        return "message";
    }


    /**
     *用户聊天列表
     * @return
     */
    @RequestMapping(value = "/msglist", method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Page<Map<String,Object>>> msglist(HttpServletRequest request) {
        /**
         * 私信传过来的id
         */
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);

        String sendid = String.valueOf(user.getId());


        /*String id = request.getParameter("umsgId");*/
        //获取最近聊天的10条记录
        String page = request.getParameter("page");
        if(page==null||"".equals(page)) page = "1";
        int _page = Integer.parseInt(page);
        int _id = Integer.parseInt(sendid);
        //分页
        Page<Map<String,Object>> pageBean = Page.builder().page(_page).rows(8).build();


        return megService.queryMsgList(_id,pageBean);
    }

    /**
     * 展示聊天的消息
     * @param request
     * @return
     */
    @RequestMapping(value="/userMsg",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String,Object>> userMsg(HttpServletRequest request){

        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);

        Long sendid = user.getId();
        String recid = request.getParameter("recid");
        int reviceid = Integer.parseInt(recid);

        return megService.queryUserMsg(sendid,reviceid);
    }

    /**
     * 发消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/sendMsg", method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> sendMsg(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        //接收者id
        String recvid = request.getParameter("recvid");
        //发送的信息
        String message = request.getParameter("message");
        int reviceid = Integer.parseInt(recvid);

        return megService.sendMsg(sendid,recvid,message);
    }

    /**
     * 加入黑名单
     * @param request
     * @return
     */
    @RequestMapping(value = "/black",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> black(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        String bid = request.getParameter("bid");
        int _bid = Integer.parseInt(bid);
        return megService.blackUser(sendid,_bid);
    }

    /**
     * 拉黑列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/myBlack",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> myBlack(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        //获取10条记录拉黑记录
        String page = request.getParameter("page");
        if(page==null||"".equals(page)) page = "1";
        int _page = Integer.parseInt(page);
        //分页
        Page<Map<String,Object>> pageBean = Page.builder().page(_page).rows(10).build();

        return megService.querymyBlack(sendid,pageBean);
    }

    /**
     * 移除拉黑
     * @param request
     * @return
     */
    @RequestMapping(value = "/removeBlack",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> removeBlack(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        String reb = request.getParameter("reb");
        int _reb = Integer.parseInt(reb);


        return megService.removeBlack(sendid,_reb);
    }

    /**
     * 未读数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/unRead",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> unread(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        return megService.queryunRead(sendid);
    }

    /**
     * 已读不回
     * @param request
     * @return
     */
    @RequestMapping(value = "/readed",method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<Map<String, Object>> readed(HttpServletRequest request) {
        // 获取当前登录的用户信息
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //发送者id  当前登录用户
        Long sendid = user.getId();
        String cRid = request.getParameter("cRid");
        int upid = Integer.parseInt(cRid);


        return megService.upreaded(sendid,upid);
    }



    /**
     * 获取用户头像
     * @return
     */
    @RequestMapping(value = "/photo",method = RequestMethod.GET)
    public HttpEntity seePhoto(){
        File file = null;
        // 获取传入的图片地址
        String photoPath = ContextUtils.getRequest().getParameter("p");

        // 判断地址是否为空
        if (photoPath==null || "".equals(photoPath)){
            String realPath = ContextUtils.getRequest().getRealPath("images/unknown.png");
            file = new File(realPath);
        }else {
            file = UploadUtils.findFile(webappRoot, photoPath);
        }
        return new HttpEntity(FileUtil.readBytes(file),"",false);
    }

    /**
     * @description 跳转私信页面
     * @date 2022/9/28 21:51
     * @auther XuHuanDong
     */
    @RequestMapping(value = "/skipletter")
    @ResponseBody
    @Authentication
    public ResultVo<String> skipLetter(HttpServletRequest request){
        //获取资源上传者id
        Long resUserId = Long.parseLong(request.getParameter("resUserId")) ;
        //获取登录用户
        User user = (User) request.getSession().getAttribute(AppConst.LOGIN_FLAG);
        if (resUserId == user.getId()){
            return ResultVo.fail("不可以私信自己").build();
        }
        //调用业务层处理
        return megService.queryBlackStatus(user.getId(),resUserId);
    }

    /**
     * @description 加载官方通知消息
     * @date 2022/10/10 21:23
     * @auther XuHuanDong
     */
    @ResponseBody
    @RequestMapping(value = "/announcement",method = RequestMethod.GET)
    public ResultVo<String> announcementMsg(){
        //调用业务层处理
        return megService.queryAllAnnouncementMsg();
    }
    /**
     * @description 加载用户所有通知信息
     * @date 2022/9/29 20:11
     * @auther XuHuanDong
     */
    @ResponseBody
    @RequestMapping(value = "/notifi",method = RequestMethod.GET)
    public ResultVo<String> notifiMessage(){
        //获取当前登录用户
        User user = (User) ContextUtils.getSession().getAttribute(AppConst.LOGIN_FLAG);
        //调用业务层处理
        return megService.queryAllNotifiMsgOfUser(user);
    }
}
